package au.edu.uq.itee.engg2800.team19;

import gnu.io.CommPortIdentifier;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ConnectionFrame extends Frame implements ActionListener {
	private RS232Conn conn;
	ArrayList<String> myports=new ArrayList<String>();
	Enumeration<CommPortIdentifier> myPorts;
	//boolean okPressed;	//Indicates wether the okay button was pressed

	//Create the panels
    private JPanel pnlEast = new JPanel(); // East
    private JPanel pnlWest = new JPanel(); // West
    private JPanel pnlSouth = new JPanel(); // South
    //Source the data for the Combo boxes
    private String[] SBlist=StopBits.enumsToStringArray();
    private String[] baudList=BaudRate.enumsToStringArray();
    private String[] DBList=DataBits.enumsToStringArray();
    private String[] FCList=FlowControl.enumsToStringArray();

    //East Panel - Combos etc
    private JComboBox comPortList;
    private JComboBox comStopBits=new JComboBox(SBlist);
    private JComboBox comBaudRate=new JComboBox(baudList);
    private JComboBox comDataBits=new JComboBox(DBList);
    private JComboBox comFlowControl=new JComboBox(FCList);
    //West Panel - labels
	private JLabel lblComPort=new JLabel("COM PORT");
	private JLabel lblbaudRate=new JLabel("Baud Rate");
	private JLabel lblStopBits=new JLabel("Stop Bits");
	private JLabel lblDataBits=new JLabel("Data Bits");
	private JLabel lblFlowControl=new JLabel("Flow Control");

	//The South Panel - buttons
	private JButton btnConnect=new JButton("Connect");

	//parent
	//private JFrame parent;

	//The Constructor
	public ConnectionFrame(JFrame parent,RS232Conn connection){

		super("Connection Properties");
		super.setResizable(false);
		//this.parent=parent;
		this.conn=connection;
		loadComPorts();
		this.add(pnlWest, BorderLayout.WEST);
		pnlWest.setLayout(new BoxLayout(pnlWest,BoxLayout.Y_AXIS));
		this.add(pnlSouth, BorderLayout.SOUTH);
		this.add(pnlEast, BorderLayout.EAST);
		pnlEast.setLayout(new BoxLayout(pnlEast,BoxLayout.Y_AXIS));
		Object[] list = myports.toArray();
		comPortList= new JComboBox(list);

		pnlWest.add(Box.createVerticalStrut(8));
		pnlWest.add(lblComPort);
		pnlWest.add(Box.createVerticalStrut(8));
		pnlWest.add(lblbaudRate);
		pnlWest.add(Box.createVerticalStrut(8));
		pnlWest.add(lblDataBits);
		pnlWest.add(Box.createVerticalStrut(8));
		pnlWest.add(lblStopBits);
		pnlWest.add(Box.createVerticalStrut(8));
		pnlWest.add(lblFlowControl);

		pnlEast.add(comPortList);
		pnlEast.add(comBaudRate);
		pnlEast.add(comDataBits);
		pnlEast.add(comStopBits);
		pnlEast.add(comFlowControl);
		pnlSouth.add(btnConnect);
		btnConnect.addActionListener(this);


		this.setLocationRelativeTo(parent);
    	this.pack();
    	comPortList.setSelectedIndex(0);
    	comPortList.addActionListener(this);

    	this.addWindowListener(new WindowAdapter(){
    	      public void windowClosing(WindowEvent e){
    	        setVisible(false);
    	      }
    	    });
	}

	private void loadComPorts(){

        myPorts=new RS232Ports().list();
        if (!(myPorts.hasMoreElements())){
        	myports.add("");
        }

        while(myPorts.hasMoreElements()){
   		 	myports.add(((CommPortIdentifier)myPorts.nextElement()).getName());
        }
    }

	@Override
	public void actionPerformed(ActionEvent evt) {
	    Object source = evt.getSource();
	    if (source == btnConnect) {
	    		//okPressed=true;
		    conn.setSerialPort(myports.get(comPortList.getSelectedIndex()));
		    conn.setBaudRate(comBaudRate.getSelectedIndex());
		    conn.setDataBits(comDataBits.getSelectedIndex());
		    conn.setStopBits(comStopBits.getSelectedIndex());
		    conn.setFlowControl(comFlowControl.getSelectedIndex());
		    try {
				conn.connect();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	    	this.setVisible(false);
	    	}
	    }
	//}
	 public void showDialog() {

		    this.setVisible(true);
		  }
}
